package com.atguigu.srb.core.controller.admin;


import com.atguigu.common.result.exception.Assert;
import com.atguigu.common.result.result.R;
import com.atguigu.common.result.result.ResponseEnum;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author Helen
 * @since 2023-03-20
 */
@Slf4j
@Api(tags = "积分等级管理")
@RestController
@RequestMapping("/admin/core/integralGrade")
//@CrossOrigin
public class AdminIntegralGradeController {

    @Resource
    private IntegralGradeService integralGradeService;
    @GetMapping
    @ApiOperation("积分等级列表")
    public R listAll(){
        log.info("hi this is log info");
        log.warn("hi this is log info");
        log.trace("hi this is log  info");
        log.warn("hi this is log  info");
        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().date("list",list).message("获取列表成功");
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "积分删除",notes = "逻辑删除积分等级")
    public R deleteAll(@ApiParam("积分id") @PathVariable Long id){
        boolean b = integralGradeService.removeById(id);
        if (b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");

    }
    @ApiOperation("增加积分")
    @PostMapping("/save")
    public R save(@RequestBody  IntegralGrade integralGrade){
/*        if (integralGrade.getBorrowAmount()==null){
            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        }*/
        /*断言*/
        Assert.notNull(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        boolean save = integralGradeService.save(integralGrade);
        if (save){
            return R.ok().message("修改成功");

        }
            return R.error().message("修改失败");

    }
    @ApiOperation("根据id获取积分")
    @GetMapping("/get/{id}")
    public R getByid(@PathVariable Long id){
        IntegralGrade grade = integralGradeService.getById(id);
        if (grade != null){
        return R.ok().date("grad",grade).message("获取成功");
    }return R.error().message("h获取失败");
    }

    @ApiModelProperty("更新积分")
    @PostMapping("/update")
    public R updateById(@RequestBody  IntegralGrade integralGrade){
        boolean save = integralGradeService.updateById(integralGrade);
        if (save){
            return R.ok().message("更新成功");

        }
        return R.error().message("更新失败");

    }
}

